package com.nuanshui.heatedloan.credit.service.third.party;

import com.alibaba.dubbo.config.annotation.Reference;
import com.nuanshui.framework.consts.TransactionManagerType;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.constant.ServiceConstant;
import com.nuanshui.heatedloan.credit.dal.mapper.OptionalAuthMapper;
import com.nuanshui.heatedloan.credit.facade.CustomerIdCardInfoService;
import com.nuanshui.heatedloan.credit.facade.third.party.PhoneNumAuthService;
import com.nuanshui.heatedloan.credit.form.CustomerPhoneInfoForm;
import com.nuanshui.heatedloan.credit.response.RiskResponse;
import com.nuanshui.heatedloan.credit.vo.CustomerPhoneInfoVO;
import com.nuanshui.heatedloan.msgcenter.facade.VerificationFacade;
import com.nuanshui.heatedloan.msgcenter.model.AliSmsSecurityCodeTemplateEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangxw on 2017/6/30.
 */

@Service(ServiceConstant.PHONENUMAUTHSSEVICE)
@Transactional(readOnly=true,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
public class PhoneNumAuthServiceImpl implements PhoneNumAuthService {

    private  static  final Logger logger= LoggerFactory.getLogger(PhoneNumAuthServiceImpl.class);

    public static final String channelIdDefault= "11009013";

    @Autowired
    private CustomerIdCardInfoService customerIdCardInfoService;

    @Autowired
    private OptionalAuthMapper optionalAuthMapper;

    @Reference(url = GlobalParam.MSGCENTER_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.MSGCENTER_DUBBO_SERVER_VERSION )
    private VerificationFacade veriFacade;

    @Override
    @Transactional(readOnly=false,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
    public RiskResponse mobilePhoneAuth(CustomerPhoneInfoForm customerPhoneInfoForm, String smsCode)  {
        RiskResponse riskResponse=new RiskResponse();
        boolean flag=veriFacade.verifyCode(customerPhoneInfoForm.getPhone(),smsCode, AliSmsSecurityCodeTemplateEnum.ALI_PHONE_AUTH);
        //保存数据库
        if(!flag){
            riskResponse.setResultCode("20");
            return riskResponse;
        }
        //判断这个手机是不是已经存在
        CustomerPhoneInfoVO customerPhoneInfoVO=this.loadVOByUserId(customerPhoneInfoForm.getUserId());
        if(customerPhoneInfoVO!=null){
          this.updateStatusByUserId(customerPhoneInfoForm.getUserId());
        }
        this.saveCustomerPhoneInfo(customerPhoneInfoForm);
        riskResponse.setResultCode("00");
        return riskResponse;
    }

    @Override
    @Transactional(readOnly=false,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
    public CustomerPhoneInfoForm saveCustomerPhoneInfo(CustomerPhoneInfoForm customerPhoneInfoForm) {
         optionalAuthMapper.saveCustomerPhoneInfo(customerPhoneInfoForm);
        return customerPhoneInfoForm;
    }

    public CustomerPhoneInfoVO loadVOByUserId(String userId){
        return optionalAuthMapper.loadCustomerPhoneInfoVOByUserId(userId);
    }

    @Transactional(readOnly=false,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
   public void updateStatusByUserId(String userId){
        optionalAuthMapper.updatePhoneStatusByUserId(userId);
   }


    /***
     * 所有的数据
     * @return
     */
    public List<CustomerPhoneInfoVO> loadAllVO(){
        List<CustomerPhoneInfoVO> customerPhoneInfoVO=new ArrayList<CustomerPhoneInfoVO>();
        customerPhoneInfoVO=optionalAuthMapper.loadAllVO();
        return customerPhoneInfoVO;
    }

}
